package com.nihao.vido;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class MainActivity extends AppCompatActivity {
    //要发送的信息
    private String masg = "看看数据能否收到";

    //接收着的ip地址

    private DatagramSocket mDatagramSocket=null;
    private String stop = "";
    private String dwn = "stop";
    private EditText mEditText;
    private int nunber = 9090;
    private TextView mView;
    private StringBuffer buffer = new StringBuffer();
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            mView.setText(buffer);

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mEditText = (EditText) findViewById(R.id.edit);
        mView = (TextView) findViewById(R.id.text);



    }

    public void stop(View view) {
        final String trim = mEditText.getText().toString().trim();
        if (trim.isEmpty() || trim == null) {
            Toast.makeText(this, "接受者的IP地址不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        //开启子线程
        new Thread(){
            @Override
            public void run() {
                try {
                    //1.指定端口号
                    mDatagramSocket = new DatagramSocket(0);

                    while (true) {
                        if (stop.equals("stop")) {
                            break;
                        }
                        //2,指定发送信息的长度,主机名字
                        //3连续发送用
                        DatagramPacket datagramPacket = new DatagramPacket(masg.getBytes(), masg.getBytes().length, InetAddress.getByName(trim), nunber);
                        mDatagramSocket.send(datagramPacket);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    if (mDatagramSocket!=null){
                        mDatagramSocket.close();
                    }
                }
            }
        }.start();
    }

    public void start(View view) {

        new Thread(){
            @Override
            public void run() {
                try {
                    DatagramSocket socket = new DatagramSocket(nunber);
                    DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);


                    while (true) {
                        socket.receive(packet);

                        byte[] arr = packet.getData();

                        int length = packet.getLength();

                        String s = new String(arr, 0, length);
                        buffer.append(s);

                        if (arr.length == 0 && length == 0) {
                            socket.close();
                            mHandler.sendEmptyMessage(0);
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
//hahah
            }
        }.start();
    }

}
